package com.tiyo.common.utils.crypto;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @Description DES加密工具类
 * @author yangb
 * @version v1.0
 * @date 2020年5月21日
*
 */
@Slf4j
public class DesUtil {
	private final static String DESEDE = "DESede";
	private String Algorithm = "DES";
	private final static String INSTANCE = DESEDE + "/CBC/PKCS5Padding";
	private final static String CHARSET = "utf-8";
	private byte[] cipherByte;

	public static String encrypt(byte[] src, String key) {
		try {
			Cipher cipher1 = Cipher.getInstance(INSTANCE);
			int blockSize = cipher1.getBlockSize();
			byte[] iv = new byte[blockSize];

			for (int i = 0; i < blockSize; ++i) {
				iv[i] = 0;
			}

			Cipher cipher = Cipher.getInstance(INSTANCE);
			SecretKey secretKey = new SecretKeySpec(Base64.decode(key), DESEDE);
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(1, secretKey, ivSpec);
			byte[] cipherBytes = cipher.doFinal(src);
			return Base64.encode(cipherBytes);
		} catch (Exception var9) {
			log.error("des加密失败", var9);
			return "";
		}
	}

	public static String encrypt(String src, String key) {
		try {
			return encrypt(src.getBytes(CHARSET), key);
		} catch (Exception var9) {
			log.error("des加密失败", var9);
			return "";
		}
	}

	public static String decrypt(String src, String key) {
		try {
			Cipher cipher1 = Cipher.getInstance(INSTANCE);
			int blockSize = cipher1.getBlockSize();
			byte[] iv = new byte[blockSize];

			for (int i = 0; i < blockSize; ++i) {
				iv[i] = 0;
			}

			Cipher cipher = Cipher.getInstance(INSTANCE);
			SecretKey secretKey = new SecretKeySpec(Base64.decode(key), DESEDE);
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(2, secretKey, ivSpec);
			byte[] b = cipher.doFinal(Base64.decode(src));
			return new String(b, CHARSET);
		} catch (Exception var9) {
			var9.printStackTrace();
			return "";
		}
	}

	public byte[] doDesCalc(int nMode, byte[] szKey, byte[] szPlainData) throws Exception {
		try {
			byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
			int len = szPlainData.length > 8 ? 8 : szPlainData.length;
			System.arraycopy(szPlainData, 0, data, 0, len);
			new SecureRandom();
			DESKeySpec dks = new DESKeySpec(szKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(this.Algorithm);
			SecretKey key = keyFactory.generateSecret(dks);
			Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
			int nLen = 8 - szPlainData.length;
			if (nLen > 0) {
				for (int i = 0; i < nLen; ++i) {
					szPlainData[nLen - i - 1] = 0;
				}
			}

			if (nMode == 1) {
				cipher.init(1, key);
			} else {
				cipher.init(2, key);
			}

			this.cipherByte = cipher.doFinal(data);
			return this.cipherByte;
		} catch (Exception var14) {
			throw new Exception("Do Des occurs Exception.[" + var14.getMessage() + "]");
		}
	}

	public byte[] do3DesCalc(int nMode, byte[] szKeyLeft, byte[] szKeyRight, byte[] szPlainData) throws Exception {
		try {
			if (nMode == 1) {
				this.cipherByte = this.doDesCalc(1, szKeyLeft, szPlainData);
				this.cipherByte = this.doDesCalc(2, szKeyRight, this.cipherByte);
				this.cipherByte = this.doDesCalc(1, szKeyLeft, this.cipherByte);
			} else {
				this.cipherByte = this.doDesCalc(2, szKeyLeft, szPlainData);
				this.cipherByte = this.doDesCalc(1, szKeyRight, this.cipherByte);
				this.cipherByte = this.doDesCalc(2, szKeyLeft, this.cipherByte);
			}

			return this.cipherByte;
		} catch (Exception var6) {
			throw new Exception("Do 3Des occur Exception.[" + var6.getMessage() + "]");
		}
	}

	public byte[] getKey() throws Exception {
		try {
			SecureRandom sr = new SecureRandom();
			byte[] keyBytes = new byte[8];
			sr.nextBytes(keyBytes);
			SecretKey key = new SecretKeySpec(keyBytes, "DES");
			return key.getEncoded();
		} catch (Exception var4) {
			throw new Exception("Do 3Des occur Exception.[" + var4.getMessage() + "]");
		}
	}
	public static void main(String[] args) {
		System.out.println(encrypt("jrgz", "lyy4pGK2Iw0XtQPqNGcdJd6aGUNWpw2B"));
		System.out.println(decrypt("caMRwZ9MIxc=", "lyy4pGK2Iw0XtQPqNGcdJd6aGUNWpw2B"));
		try {
			System.out.println(URLEncoder.encode("caMRwZ9MIxc=", "utf-8"));
			System.out.println(URLDecoder.decode("caMRwZ9MIxc%3D", "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(DESede_CBC_PKCS5Padding.encrypt("jrgz", "lyy4pGK2Iw0XtQPqNGcdJd6aGUNWpw2B"));
        System.out.println(encrypt("jrgzqwer819", "lyy4pGK2Iw0XtQPqNGcdJd6aGUNWpw2B"));
	}
}
